package com.hxzy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxzy.common.exception.ServiceStringException;
import com.hxzy.controller.vue.category.req.CategoryReq;
import com.hxzy.controller.vue.category.resp.CategoryResp;
import com.hxzy.entity.CategoryClass;
import com.hxzy.entity.Merchandiser;
import com.hxzy.entity.Product;
import com.hxzy.service.CategoryClassService;
import com.hxzy.mapper.CategoryClassMapper;
import com.hxzy.service.MerchandiserService;
import com.hxzy.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
* @author 11452
* @description 针对表【category_class(商品分类表)】的数据库操作Service实现
* @createDate 2023-08-28 11:54:55
*/
@Service
public class CategoryClassServiceImpl extends ServiceImpl<CategoryClassMapper, CategoryClass>
    implements CategoryClassService{

    @Autowired
    private ProductService productService;

    @Override
    public PageInfo<CategoryResp> search(CategoryReq req) {
        return PageHelper.startPage(req.getPage(),req.getSize())
                .doSelectPageInfo(()-> {
                    super.baseMapper.search(req);
                });
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deletes(Long[] ids) {
        for(Long id : ids){
           LambdaQueryWrapper<Product> queryWrapper=new LambdaQueryWrapper<>();
           queryWrapper.eq(Product::getCategoryId, id);
            long count = this.productService.count(queryWrapper);
            if(count>0){
                throw new ServiceStringException(id+"分类存在引用关系，不能删除");
            }
            //物理删除
            this.baseMapper.deleteById(id);
        }
        return true;
    }
}




